package cn.arikeky.controller;


import cn.arikeky.basic.util.AjaxResult;
import cn.arikeky.domain.Writer;
import cn.arikeky.service.WriterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/writer")
public class WriterController {

    @Autowired
    private WriterService writerService;

    // 查询全部
    @GetMapping
    public List<Writer> findAll(){
        return writerService.findWAll();
    }

    // 添加  修改
    @PutMapping
    public AjaxResult addAndUpdate(@RequestBody Writer writer){
        if (null == writer.getW_id()){
            writerService.addW(writer);
        } else {
            writerService.updateW(writer);
        }
        return  AjaxResult.me().success();
    }

    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        writerService.deleteW(id);
        return  AjaxResult.me().success();
    }

    // 根据id查询
    @GetMapping("/{id}")
    public Writer findById(@PathVariable("id") Long id){
        return writerService.findWById(id);
    }
}
